package com.deer.wms.system.manage.web;

import com.deer.wms.intercept.annotation.Authority;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.system.manage.model.address.ShippingAddress;
import com.deer.wms.system.manage.model.address.ShippingAddressCreate;
import com.deer.wms.system.manage.model.address.ShippingAddressCriteria;
import com.deer.wms.system.manage.model.address.ShippingAddressDto;
import com.deer.wms.system.manage.service.ShippingAddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by guo on 2019/11/13.
*/
@Api(description = "寄件地址api接口")
@RestController
@RequestMapping("/shipping/addresss")
public class ShippingAddressController {

    @Autowired
    private ShippingAddressService shippingAddressService;


    @ApiOperation(value = "新增寄件地址信息", notes = "新增寄件地址信息")
    @Authority
    @ApiImplicitParams( { @ApiImplicitParam( name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true ) } )
    @PostMapping("/insert")
    public Result createShippingAddress(@RequestBody ShippingAddressCreate create, @ApiIgnore @User CurrentUser currentUser) {

        shippingAddressService.createShippingAddress(create,currentUser);
        return ResultGenerator.genSuccessResult();
    }
    @ApiOperation(value = "删除寄件地址", notes = "删除寄件地址")
    @Authority
    @ApiImplicitParams( { @ApiImplicitParam( name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true ) } )
    @GetMapping("/delete")
    public Result delete(Integer id) {

        shippingAddressService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }
    @ApiOperation(value = "更新寄件地址", notes = "更新寄件地址")
    @Authority
    @ApiImplicitParams( { @ApiImplicitParam( name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true ) } )
    @PostMapping("/update")
    public Result update(@RequestBody ShippingAddress shippingAddress) {
        if(shippingAddress.getState() ==1) {
            List<ShippingAddressDto> list = shippingAddressService.findByOrganizationIdAndWareId(shippingAddress.getOrganizationId(),shippingAddress.getWareId());
            for (ShippingAddress shippingAddress1 : list){
                shippingAddress1.setState(0);
                shippingAddressService.updateById(shippingAddress1);
            }
        }
        shippingAddressService.updateById(shippingAddress);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "查询单个寄件地址", notes = "查询单个寄件地址")
    @Authority
    @ApiImplicitParams( { @ApiImplicitParam( name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true ) } )
    @GetMapping("/detail")
    public Result detail(Integer id) {
        ShippingAddress shippingAddress = shippingAddressService.getById(id);
        return ResultGenerator.genSuccessResult(shippingAddress);
    }


    @ApiOperation(value = "查询寄件地址信息", notes = "查询寄件地址信息")
    @Authority
    @ApiImplicitParams( { @ApiImplicitParam( name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true ) } )
    @GetMapping("/list")
    public Result list(ShippingAddressCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.SERVICE_ERROR,"未登录错误",null);
        }
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<ShippingAddressDto> list = shippingAddressService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
